﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenExpressApp.MetaAttribute;
using Csla.Wpf;
using OpenExpressApp.Command;
using Csla.Core;
using System.ComponentModel;
using System.Windows;
using OpenExpressApp.Template;
using OpenExpressApp.Types;
using Csla;
using OpenExpressApp.WPF.Controls;
using System.Collections.ObjectModel;
using log4net;
using System.Collections;
using OpenExpressApp.MetaModel;
using Avalon.Windows.Controls;
using OpenExpressApp.WPF.Templete;
using OpenExpressApp.Module;
using OpenExpressApp.Module.WPF;
using System.Windows.Controls;
using Ricciolo.Controls;
using System.Reflection;
using System.Windows.Markup;
using System.Diagnostics;
using AvalonDock;
using OpenExpressApp.Module.WPF.Editors;
using OpenExpressApp.Module.WPF.Command.WpfReport;

using System.Windows.Media;

namespace OpenExpressApp.WPF.Command
{
    class ViewExtCommand
    {
        [Command(CommandNames.SelectAll, Label = "选择全部", ToolbarType = ToolbarType.Manual, ToolTip = "选择全部")]
        public class SelectAllCommand : WPFListViewCommand
        {
            public override void Execute(ListObjectView view)
            {
                view.SelectAll();
            }
        }

        [Command(CommandNames.MaxShowView, ImageName="MaxShowView.bmp", Label = "最大化", ToolbarType = ToolbarType.Manual, IngoreIsExecuting = true, ToolTip = "最大化")]
        public class MaxShowViewCommand : WPFListViewCommand
        {
            public override bool CanVisible(ListObjectView view)
            {
                return (null != view) && view.InOrder;
            }

            private double GetInitScale(FrameworkElement fe)
            {               
                FrameworkElement v = LogicalTreeHelper.GetParent(fe) as FrameworkElement;
                if (null != v)
                {
                    if (v.LayoutTransform is ScaleTransform)
                        return (v.LayoutTransform as ScaleTransform).ScaleX;
                    else 
                        return GetInitScale(v);
                }
                return 1;
            }

            public override void Execute(ListObjectView view)
            {
                TabItem parent = (view.Control as FrameworkElement).GetLogicalParent<TabItem>();
                FrameworkElement curControl = parent.Content as FrameworkElement;
                double initScale = GetInitScale(curControl);  //必须放在 parent.Content = null;之前
                parent.Content = null;
                DialogWorkspace.ShowView(curControl, new DialogOptions()    //弹出弹出窗体
                {
                    AllowResize = true,
                    DialogTitle = view.BOsPropInfo.Label,
                    DialogSize = new Size(curControl.ActualWidth, curControl.ActualHeight),
                    HideOkCancelButtons = true,
                    WindowState = WindowState.Maximized,
                    ZoomScale = initScale,
                }, null);

                curControl.LayoutTransform = null;
                parent.Content = curControl;
            }
        }

        [Command(CommandNames.ChildShowView, ImageName = "OpenFile.bmp", Label = "显示子表", ToolbarType = ToolbarType.Manual, IngoreIsExecuting = true, ToolTip = "显示子表")]
        public class ChildShowViewCommand : WPFListViewCommand
        {
            public override bool CanExecute(ListObjectView view)
            {                
                return (null != view) && (view.CurrentObject != null);
            }

            public override void Execute(ListObjectView view)
            {
                Dictionary<string, object> commandParams = this.CustomParams;
                object childType = null;
                ListObjectView childView = null;
                this.CustomParams.TryGetValue(CommandCustomParams.ChildViewName, out childType);
                Debug.Assert(childType != null);
                childView = view.GetChildView(childType as Type, true) as ListObjectView;
                childView.IsActive = true;
                ApplicationModel.Commands[CommandNames.MaxShowView].Execute(childView);
                
            }
        }

        //#region 通用打印

        //[Command(CommandNames.Report, Label = "打印", ToolTip = "打印", CommandType = CommandType.Menu,ToolbarType=ToolbarType.Any)]
        //public class PrintCommand : WPFListViewCommand
        //{
        //    public override bool CanVisible(ListObjectView view)
        //    {
        //        if (view == null)
        //        {
        //            return false;
        //        }
        //        else
        //        {
        //            return (view.Control is DataGrid) || (view.Control is MultiObjectTreeView) || (view.Control is TreeListView);
        //        }
        //    }

        //    public override bool CanExecute(ListObjectView view)
        //    {
        //        return base.CanExecute(view) && view.Data != null;
        //    }

        //    public override void Execute(ListObjectView view)
        //    {
        //        ReportWindow report = new ReportWindow(view);
        //        report.Show();
        //    }
        //}

        //#endregion
    }
}
